#!/bin/bash
#所有者：北城半夏
#
#当前版本
#v1.0.1
#######################################################################
#定义变量
basepath=$(cd `dirname $0`; pwd)
#ansible离线安装包的名称
ansible_offline="ansible2.9_yum.tar.gz"
ansible_name="ansible"
#ansible离线安装包的路径
ansible_opt="/opt"
# get local ip
LAN_IP=$(ip addr| grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|cut -d "/" -f 1 | awk '{print $1}' | head -1)
######################################################################
#日志输出
info2(){
  echo -e "\033[31m 【`hostname` `whoami` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[34m$1\033[0m "
}
info(){
  echo -e "\033[35m 【`hostname` `whoami` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[36m$1\033[0m "
}
info1(){
  echo -e "\033[91m 【`hostname` `whoami` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[35m$1\033[0m"
}
######################################################################

set_firewalld(){
# stop firewalld and selinux
    systemctl stop firewalld && systemctl disable firewalld
    getenforce | grep -iq 'Enforcing' && setenforce 0 
    sed -i 's|SELINUX=\(.*\)|SELINUX=disabled|g' /etc/selinux/config
}
# deploy机器检测python环境
python(){
python2 -V &>/dev/null
if [ $? -ne 0 ];then
    if cat /etc/redhat-release &>/dev/null;then
        yum install gcc openssl-devel bzip2-devel 
        wget https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tgz
        tar xzf Python-2.7.16.tgz
        cd Python-2.7.16
        ./configure --enable-optimizations
        make altinstall
        ln -s /usr/bin/python2.7 /usr/bin/python
        cd -
    else
        apt-get install -y python2.7 && ln -s /usr/bin/python2.7 /usr/bin/python
    fi
fi
}
#yum安装ansible
yum_ansible(){
    if [ -e ${ansible_opt}/${ansible_offline} ];then
        info "使用离线安装包，安装ansible，请等待！！！"
        info "Check the offline installation package"
        echo "${ansible_opt}/${ansible_offline}"
        if [ -e ${ansible_opt}/${ansible_offline} ];then
                cd ${ansible_opt}
                tar -xf ${ansible_offline} 
                cd ${ansible_name}
                yum -y install ./*.rpm  >/dev/null
                [ $? -eq 0 ] && info "Ansible is installed successfully"
        else
                info2 "Ansible is installed fail"
       fi
    else
   ping -c 1   www.baidu.com &>/dev/null
   if [ $? -eq 0 ] ;then
       if [ `rpm -qa | grep ansible|wc -l` -eq 1 ];then
            info1 "ansible 已经安装，请勿重新安装！！！"
        else
            info "在线安装ansible,请等待！！！"
         if [ `rpm -qa | grep epel-release|wc -l ` -ne 1 ];then
              rpm -ivh https://mirror.webtatic.com/yum/el7/epel-release.rpm &>/dev/null
              yum makecache 
              yum install  -y ansible sshpass   
              [ $? -eq 0 ] && info "Ansible is installed successfully"
         else
              info "epel-release been installed"
              info "使用在线安装，请等待！！！"
              yum makecache  
              yum install -y ansible sshpass -y 
              [ $? -eq 0 ] && info "Ansible is installed successfully"
         fi
       fi  
    fi
 
fi
}
#开机设置防火墙、selinux
set_firewalld
#deploy机器检测python环境
python
yum_ansible

sed -i "s/#host_key_checking = False/host_key_checking = False/g" /etc/ansible/ansible.cfg
sed -i "/deprecation_warnings=False/d" /etc/ansible/ansible.cfg
echo "deprecation_warnings=False" >>/etc/ansible/ansible.cfg
